
// This do-file replicates the chamber analysis in Chapter 4

* Bring in the dataset indicating which laws states had across different years
* and rename variables to merge with the Shor McCarty Dataset
use masterdataset, clear
ren statabbr state
sort state year
save masterdataset2, replace

* Merge Shor McCarty Dataset with our dataset on campaign finance lawas
use "shor mccarty 1993-2013 state aggregate data public July 2014.dta", clear
ren st state
sort state year
merge state year using masterdataset2
drop if _m==2 & year~=1992
drop _m

* Drop Nebraska since it is nonpartisan
drop if state=="NE"

* Generate a variable that is 1 if the state limited either contributions to or from parties
* 0 if there were no limits
gen partylims=1 if l_party_cand==1 | l_ind_party==1
replace partylims=0 if l_party_cand==0 & l_ind_party==0

* Generate a variable indicating whether state was in top 20 most professional
gen professional2=1 if state=="CA" | state=="NY" | state=="PA" | state=="MI" | state=="IL" | state=="MA" | state=="WI" | state=="OH" | state=="FL" | state=="NJ" | state=="AK" | state=="DE" | state=="MN" | state=="TX" | state=="WA" | state=="CT" | state=="HI" | state=="NC" | state=="SC" | state=="AZ" 


* Fill in the non-election year gaps in laws variables
sort state year
replace orglims=orglims[_n-1] if orglims==. & state==state[_n-1] & year<=2013
replace indlims=indlims[_n-1] if indlims==. & state==state[_n-1] & year<=2013
replace partylims=partylims[_n-1] if partylims==. & state==state[_n-1] & year<=2013


* The following commands stack the house and senate observations
save temp, replace
keep if h_diffs~=. 
ren h_diffs med_diff
gen chamber=1
save house, replace

use temp, clear
keep if s_diffs~=.
ren s_diffs med_diff
gen chamber=2
append using house

* The following commands create a unique identifier for each state/chamber
encode state, gen(stateno)
egen temp=concat(stateno chamber)
gen st_chamber=real(temp)

* Set the data as panel data: units=chambers, time=year 
xtset st_chamber year

* Difference of means tests for all Legislatures
ttest med_diff if chamber==1, by(partylims)
ttest med_diff if chamber==2, by(partylims)
ttest med_diff, by(partylims)

* Difference of means tests for Professional Legislatures
ttest med_diff if chamber==1 & professional2==1, by(partylims)
ttest med_diff if chamber==2 & professional2==1, by(partylims)
ttest med_diff, by(partylims)

* Cross-section time series ANALYSIS OF POLARIZATION (First column of Table 4.2)
xtreg med_diff l.med_diff year partylims indlims orglims, fe
est store allchambers

* ANALYSIS OF POLARIZATION (Professional Legislatures) (Second column of Table 4.2)
xtreg med_diff l.med_diff year partylims indlims orglims if professional2==1, fe
est store professionalchambers

* This command collects the results shown in Table 4.2
estout allchambers professionalchambers using temp.txt,  c(b(star fmt(3)) se(par fmt(3))) stats(N r2) starlevels(* 0.05) l style(tab) replace

/*
* This commented out section will graph predicted effects showin in Figure 4.5

sort st_chamber year
by st_chamber: gen lag_med_diffs=med_diff[_n-1]

save temp, replace

xtreg med_diff lag_med_diff year partylims indlims orglims if professional2==1, fe
keep med_diff lag_med_diff year partylims indlims orglims

drop if _n>=1

input 
1996 . 1 1 0 1.483846
end
predict nolimits
set obs 10
replace year=year[_n-1]+1 if year==.
replace indlims=1
replace orglims=1
replace partylims=0
replace lag_med_diff= nolimits[_n-1] if lag_med_diff==.
drop nolimits
predict nolimits
replace lag_med_diff= nolimits[_n-1] if lag_med_diff==.
drop nolimits
predict nolimits
replace lag_med_diff= nolimits[_n-1] if lag_med_diff==.
drop nolimits
predict nolimits
replace lag_med_diff= nolimits[_n-1] if lag_med_diff==.
drop nolimits
predict nolimits
replace lag_med_diff= nolimits[_n-1] if lag_med_diff==.
drop nolimits
predict nolimits
replace lag_med_diff= nolimits[_n-1] if lag_med_diff==.
drop nolimits
predict nolimits
replace lag_med_diff= nolimits[_n-1] if lag_med_diff==.
drop nolimits
predict nolimits
replace lag_med_diff= nolimits[_n-1] if lag_med_diff==.
drop nolimits
predict nolimits
replace lag_med_diff= nolimits[_n-1] if lag_med_diff==.
drop nolimits
predict nolimits

save nolimits, replace

use temp, clear

xtreg med_diff lag_med_diff year partylims indlims orglims if professional2==1, fe

keep med_diff lag_med_diff year partylims indlims orglims

drop if _n>=1

input 
1996 . 1 1 1 1.483846
end
predict limits
set obs 10
replace year=year[_n-1]+1 if year==.
replace indlims=1
replace orglims=1
replace partylims=1
replace lag_med_diff= limits[_n-1] if lag_med_diff==.
drop limits
predict limits
replace lag_med_diff= limits[_n-1] if lag_med_diff==.
drop limits
predict limits
replace lag_med_diff= limits[_n-1] if lag_med_diff==.
drop limits
predict limits
replace lag_med_diff= limits[_n-1] if lag_med_diff==.
drop limits
predict limits
replace lag_med_diff= limits[_n-1] if lag_med_diff==.
drop limits
predict limits
replace lag_med_diff= limits[_n-1] if lag_med_diff==.
drop limits
predict limits
replace lag_med_diff= limits[_n-1] if lag_med_diff==.
drop limits
predict limits
replace lag_med_diff= limits[_n-1] if lag_med_diff==.
drop limits
predict limits
replace lag_med_diff= limits[_n-1] if lag_med_diff==.
drop limits
predict limits

append using nolimits
input 
1995 . . . . . 1.483846 1.483846
end
sort year
twoway connected nolimits year, clp(shortdash) clc(black) mc(black) ms(S) || connected limits year, mc(black) clc(black) legend(lab(1 "Parties Unlimited") lab(2 "Parties Limited")) ytitle("Distance between Party Medians") xtitle("Year") scheme(s1color)
graph export fig_4_5.eps, replace

*/

* The following commands create Figure 4.6

* Keep only observations between 1997 and 2008, since that is the period during which
* most states have observations
drop if year<1997 | year>2008

* Drop states that changed their laws between 1997 and 2008
bysort st_chamber: egen laws=mean(partylims)
save temp, replace
keep if laws==0 | laws==1

* Keep only observations for 1997 and 2007
keep if year==1997 | year==2007
sort st_chamber year

* Create a variable measuring the difference in polarization from 1997 to 2007
gen polarization_change=med_diff-med_diff[_n-1] if st_chamber==st_chamber[_n-1]
keep if year==2007

* Generate a string variable representing the party limits variable
gen partylaws="No Limits" if partylims==0
replace partylaws="Limited" if partylims==1

* Create the bar graph showing bi-variate change in polarization (Figure 4.6)
graph bar polarization_change if professional2==1, over(partylaws) scheme(s1color) ylabel(0 .05  .1  .15 , nogrid) ytitle("Increase in Polarization 1997 - 2007")
graph export fig_4_6.eps, replace


